package com.hzit.order.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.ref.PhantomReference;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
	public static ThreadLocal<String> loginName = new ThreadLocal<>();
	//1. 处理器方法执行前调用
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		//1.1 得到session
		HttpSession session = request.getSession();
		Object name = session.getAttribute("name");
		// System.out.println("LoginInterceptor--->name:" + name.toString());
		if(name != null){
			//1.2 向本地线程中添加登录用户
			loginName.set(name.toString());
			return true;
		}
		//1.3 如果当前用户没有登录，就到登录页面去
		 response.sendRedirect("http://auth.hzit.com/gitee/login?returnUrl=http://order.hzit.com");
		return false;
	}
}
